package com.zj.common;

import javafx.application.Application;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUtil {

    public String upload(MultipartFile file) throws IOException {
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        String UPLOAD_PATH = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+"\\src\\main\\resources\\static\\upload\\";
        //获取文件后缀
        final String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.')+1);
        //设置新的文件名字
        String fileName = Utility.getUUID()+"."+fileSuffix;
        //缓存写入文件
        File descFile = new File(UPLOAD_PATH,fileName);
        if(!descFile.exists()){
            //创建目录
            descFile.mkdir();
        }
        file.transferTo(descFile);
        String url = "/upload/" + fileName;
        return url;
    }
}
